package 二叉搜索树的最小绝对差;

import java.util.Stack;

public class Solution2 {
    public int getMinimumDifference(TreeNode root) {
        // 迭代法
        Stack<TreeNode> stack = new Stack<>();
        TreeNode p = root, pre = null;
        int result = Integer.MAX_VALUE;
        while (p != null || !stack.isEmpty()) {
            if (p != null) {
                // left
                stack.push(p);
                p = p.left;
            } else {
                // in
                p = stack.pop();
                if (pre != null) {
                    result = Math.min(result, p.val - pre.val);
                }
                pre = p;
                // right
                p = p.right;
            }
        }
        return result;
    }
}
